home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / daymisckit_proj / daymisckit-1 / DAYStopwatch.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  133 lines

  1. //
  2. //    DAYStopwatch.m -- a generic class to time things (measure)
  3. //        Written by Don Yacktman (c) 1993 by Don Yacktman.
  4. //                Version 1.0.  All rights reserved.
  5. //
  6. //        This is a free object!  Contact the author for the latest version.
  7. //        Don Yacktman, 4279 N. Ivy Lane, Provo, UT, 84604
  8. //        e-mail:  Don_Yacktman@byu.edu
  9. //
  10. //        You may use and copy this class freely as long as you
  11. //        comply with the following terms:
  12. //            (1) Do not remove the author's name or any of the
  13. //                copyright notices from this file.
  14. //            (2) If you redistribute an application which uses this
  15. //                object, you must either include the source code for
  16. //                this object with the application or state in your
  17. //                application's documentation that you (a) use this
  18. //                object and (b) where to obtain the source code for
  19. //                the object.
  20. //            (3) In no way shall the author or his employer(s) be held
  21. //                responsible for any damages caused by what this object
  22. //                does or does not do.
  23. //            (4) You have no warranty whatsoever that this object is
  24. //                good for any purpose at all.  If you find it useful
  25. //                for something, consider yourself lucky and leave it at that.
  26. //
  27.  
  28. #import <daymisckit/DAYStopwatch.h>
  29.  
  30. @implementation DAYStopwatch
  31.  
  32. - init
  33. {
  34.     id ret = [super init];
  35.     [self clearTiming:self];
  36.     isRelative = YES;
  37.     paused = YES;
  38.     return ret;
  39. }
  40.  
  41. - setRelative:(BOOL)t // overridden
  42. {
  43.     isRelative = YES;
  44.     return self;
  45. }
  46.  
  47.  
  48. - clearTiming:sender
  49. {
  50.     struct timezone tzp;
  51.     gettimeofday(&startTime, &tzp);
  52.     startTime.tv_sec = 0;
  53.     startTime.tv_usec = 0;
  54.     [[[[[[[[self setMicrosecond:0] setSecond:0] setMinute:0] setHour:0]
  55.             setDay:0] setMonth:0] setYear:0] calcDayOfWeek];
  56.     return self;
  57. }
  58.  
  59. - startTiming:sender
  60. {
  61.     [self clearTiming:sender];
  62.     [self continueTiming:sender];
  63.     return self;
  64. }
  65.  
  66. - continueTiming:sender
  67. {
  68.     struct timezone tzp;
  69.     if (!paused) return self;    // already running
  70.     paused = NO;
  71.     gettimeofday(&startTime, &tzp);
  72.     return self;
  73. }
  74.  
  75. - pauseTiming:sender
  76. {
  77.     [self calcElapsedTime:sender];
  78.     paused = YES;
  79.     return self;
  80. }
  81.  
  82. - calcElapsedTime:sender
  83. {    // adds elapsed time to the time's value and continues
  84.     struct timeval endTime;
  85.     struct timezone tzp;
  86.     
  87.     if (!paused) { // if we're paused, no time to add.
  88.         gettimeofday(&endTime, &tzp);
  89.         [self addMicroseconds:(endTime.tv_usec - startTime.tv_usec)];
  90.         [self addSeconds:(endTime.tv_sec - startTime.tv_sec)];
  91.         startTime.tv_sec = endTime.tv_sec;
  92.         startTime.tv_usec = endTime.tv_usec;
  93.     }
  94.     return self;
  95. }
  96.  
  97. - read:(NXTypedStream *)stream
  98. {
  99.     [super read:stream];
  100.     NXReadTypes(stream, "cll",
  101.             &paused, &startTime.tv_sec, &startTime.tv_usec);
  102.     return self;
  103. }
  104.  
  105. - write:(NXTypedStream *)stream
  106. {
  107.     [super write:stream];
  108.     NXWriteTypes(stream, "cll",
  109.             &paused, &startTime.tv_sec, &startTime.tv_usec);
  110.     return self;
  111. }
  112.  
  113. // NXTransport protocol implementation:
  114. - encodeUsing:(id <NXEncoding>)portal
  115. {
  116.     [super encodeUsing:portal];
  117.     [portal encodeData:&paused            ofType:"c"];
  118.     [portal encodeData:&startTime.tv_sec  ofType:"i"];
  119.     [portal encodeData:&startTime.tv_usec ofType:"i"];
  120.     return self;
  121. }
  122.  
  123. - decodeUsing:(id <NXDecoding>)portal
  124. {
  125.     [super decodeUsing:portal];
  126.     [portal decodeData:&paused            ofType:"c"];
  127.     [portal decodeData:&startTime.tv_sec  ofType:"i"];
  128.     [portal decodeData:&startTime.tv_usec ofType:"i"];
  129.     return self;
  130. }
  131.  
  132. @end
  133.